<?php

namespace App\Models\Common;

use App\Helpers\Code;
use App\Models\Account\UserPermission;
use App\Models\BaseModel;

class MenuAction extends BaseModel
{

    protected $tables = 'menu_actions';
    protected $fillable = ['menu_id', 'title', 'code', 'description', 'data_scope'];
    public $params = [];
    public $sorters = [];

    public function menu()
    {
        return $this->belongsTo('App\Models\Common\Menu', 'menu_id');
    }

    public function permissions()
    {
        return $this->belongsToMany('App\Models\Account\UserPermission', 'menu_action_map_permission', 'action_id', 'permission_id')->withTimestamps();
    }

    public function scopes()
    {
        return $this->belongsToMany('App\Models\Common\DataScope','user_group_menu_action_scopes', 'action_id', 'scope_id');
    }

    /**
     * 菜单添加功能
     * @param $data
     * @return int
     */
    public function addAction($data)
    {
        $title = $this->where('menu_id', $data['menu_id'])->where('title', $data['title'])->count();

        if ($title > 0) {
            return Code::INSIDE_ERROR;
        }

        $data['data_scope'] = $this->getDataScope();
        return $this->create($data);
    }

    /**
     * 菜单删除功能
     * @param $id
     * @return null
     * todo:待完善
     */
    public function deleteAction($id)
    {
        $model = $this->find($id);

        if (!$model) {
            return null;
        } else if ($model->permissions()->count() > 0) {
            return Code::INSIDE_ERROR;
        }

        $model->permissions()->detach();

        return $model->delete();
    }

    /**
     * 菜单修改功能
     * @param $id
     * @param $data
     * @return int|null
     */
    public function editAction($id, $data)
    {
        $model = $this->find($id);

        if (!$model) {
            return null;
        }

        if (isset($data['title'])) {
            $title = $this->where('menu_id', $model->menu_id)->where('id', '!=', $id)->where('title',$data['title'])->count();

            if ($title > 0) {
                return Code::INSIDE_ERROR;
            }
        } else {
            $data['title'] = $model->title;
        }

        $data['code'] = $data['code'] ?? $model->code;

        return $model->updateOrCreate(['id' => $id], $data);
    }

    /**
     * 为指定的功能添加对应的接口
     * @param $id
     * @param $data
     * @return bool|int|null
     */
    public function editActionPermission($id, $data)
    {
        $model = $this->find($id);

        if (!$model) {
            return null;
        }

        $permission = UserPermission::whereIn('user_permissions.id', $data)->count();

        if (count($data) !== $permission) {
            return Code::INSIDE_ERROR;
        }

        $model->permissions()->sync($data);

        return Code::INSIDE_TRUE;
    }

    /**
     * 获取指定的功能对应的接口
     * @param $id
     * @return null
     */
    public function getActionPermissionAll($id)
    {
        $model = $this->find($id);

        if (!$model) {
            return null;
        }

        $data = $model->with('permissions:user_permissions.id,title')->select(['id', 'title', 'code'])->where('id', $id)->first()->toArray();

        if (count($data['permissions']) > 0) {
            $response = array_column($data['permissions'], 'id');
        } else {
            $response = [];
        }

        return $response;
    }

}
